package com.cskaoyan.controller.wxcoupon;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.bean.vo.wxcoupon.CouponMyListVo;
import com.cskaoyan.bean.vo.wxcoupon.CouponSelectListVo;
import com.cskaoyan.service.wxcoupon.CouponService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @auther zk
 * @date 2022/7/21 15:18
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    CouponService couponService;

    @Autowired
    MemoryBean memoryBean;


    /**
     * @param status优惠卷状态 0可用 1过期 2下架 page:页码 limit:每页大小
     * @return 优惠卷信息分页
     * @auther zk
     * @description admin/coupon/mylist 获取优惠卷列表
     */
    @RequestMapping("mylist")
    public BaseRespVo couponMylist(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, Short status, Integer limit, Integer page) {

        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return BaseRespVo.ok(null, "token失效，请重新登录后再试", 0);
        }
        CouponMyListVo couponMyListVo = couponService.couponMyList(userId, status, limit, page);
        return BaseRespVo.ok(couponMyListVo, "成功", 0);
    }

    @RequestMapping("receive")
    public BaseRespVo couponReceive(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, @RequestBody Map map) {
        Integer couponId = (Integer) map.get("couponId");
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return BaseRespVo.ok(null, "token失效，请重新登录后再试", 0);
        }
        Integer flag = couponService.couponReceive(couponId, userId);
        if (flag == 0) {
            return BaseRespVo.ok(null, "成功", 0);
        } else if (flag == 1) {
            return BaseRespVo.error(740, "优惠卷已领完");
        } else {
            return BaseRespVo.error(740, "只能领取" + -flag + "张");
        }
    }

    @RequestMapping("selectlist")
    public BaseRespVo couponSelectList(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, Integer cartId, Integer grouponRulesId) {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        CouponSelectListVo couponSelectListVo = couponService.couponSelectList(userId, cartId, grouponRulesId);
        return BaseRespVo.ok(couponSelectListVo, "成功", 0);
    }

    @RequestMapping("exchange")
    public BaseRespVo couponExchange(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, @RequestBody Map map) {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return BaseRespVo.ok(null, "成功", 0);
        }
        String code = (String) map.get("code");
        Integer flag = couponService.couponExchange(userId, code);
        if (flag == 0) {
            return BaseRespVo.ok(null, "成功", 0);
        } else if (flag == 1) {
            return BaseRespVo.error(742, "兑换码错误");
        } else {
            return BaseRespVo.error(742, "优惠卷已兑完");
        }
     }


}
